//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.aspect;

import com.alibaba.fastjson.JSON;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.HandlerMapping;
import org.yaukie.base.annotation.OperLog;
import org.yaukie.base.constant.SysConstant.OperResult;
import org.yaukie.base.core.entity.XOperLog;
import org.yaukie.base.system.ASyncFactory;
import org.yaukie.base.system.ASyncManager;
import org.yaukie.base.util.IpUtil;
import org.yaukie.base.util.ServletUtils;
import org.yaukie.base.util.SpringContextUtil;
import org.yaukie.base.util.StringTools;

@Component
@Aspect
public class OperationLogAop {
    private static final Logger log = LoggerFactory.getLogger(OperationLogAop.class);
    @Autowired
    private ASyncManager aSyncManager;
    @Value("${swagger.version}")
    private String operVer;

    public OperationLogAop() {
    }

    @AfterReturning(
            value = "org.yaukie.base.arch.Architecture.saveOperLogPointCut()",
            returning = "params"
    )
    public void saveOperLogAdvice(JoinPoint joinPoint, Object params) throws Throwable {
        this.doInsertOperLog(joinPoint, params, (Throwable)null);
    }

    @AfterThrowing(
            value = "org.yaukie.base.arch.Architecture.operExceptionLogPoinCut()",
            throwing = "t"
    )
    public void operExceptionLogAdvice(JoinPoint joinPoint, Throwable t) throws Throwable {
        this.doInsertOperLog(joinPoint, (Object)null, t);
    }

    private void doInsertOperLog(JoinPoint joinPoint, Object params, Throwable t) {
        try {
            HttpServletRequest request = SpringContextUtil.getRequest();
            MethodSignature signature = (MethodSignature)joinPoint.getSignature();
            Method method = signature.getMethod();
            if (StringTools.isNull(method)) {
                return;
            }

            OperLog operLog = (OperLog)method.getAnnotation(OperLog.class);
            XOperLog xOperLog = new XOperLog();
            if (StringTools.isNotNull(operLog)) {
                String moduleName = operLog.moduleName();
                String operatorName = operLog.operationType().name();
                String clientType = operLog.clientType().name();
                String methodName = method.getName();
                String requestType = request.getMethod();
                String jsonResult = JSON.toJSONString(params);
                xOperLog.setMethod(joinPoint.getTarget().getClass().getName() + "." + methodName + "()");
                xOperLog.setRequestType(requestType);
                xOperLog.setModuleName(moduleName);
                xOperLog.setOperatorName(operatorName);
                xOperLog.setClientType(clientType);
                //xOperLog.setOperUserName(request.getSession().getAttribute("currentUserName").toString());
                //xOperLog.setOperDeptName(request.getSession().getAttribute("currentDeptName").toString());
                xOperLog.setOperUrl(request.getContextPath() + request.getRequestURL());
                xOperLog.setOperIp(IpUtil.getIpAddr(request));
                xOperLog.setOperLocation(IpUtil.getRealAddressByIP(IpUtil.getIpAddr(request)));
                xOperLog.setStatus(OperResult.SUCCESS.name());
                if (operLog.ifSaveRequestParams()) {
                    this.saveRequestValue(joinPoint, xOperLog, request);
                }

                if (StringTools.isNotNull(t)) {
                    xOperLog.setStatus(OperResult.FAIL.name());
                    StringWriter sw = new StringWriter();
                    t.printStackTrace(new PrintWriter(sw));
                    xOperLog.setErrorMsg(sw.toString().substring(0, 1800));
                }

                xOperLog.setJsonResult(jsonResult);
                this.aSyncManager.execute(ASyncFactory.recordBehaviorInfo(xOperLog));
            }
        } catch (Exception var16) {
            if (log.isErrorEnabled()) {
                StringWriter stringWriter = new StringWriter();
                var16.printStackTrace(new PrintWriter(stringWriter));
                log.error("日志记录通知异常{}....", stringWriter.toString().substring(0, 1800));
            }
        }

    }

    private void saveRequestValue(JoinPoint joinPoint, XOperLog xOperLog, HttpServletRequest request) throws Exception {
        String requestMethod = xOperLog.getRequestType();
        if (!HttpMethod.PUT.name().equals(requestMethod) && !HttpMethod.POST.name().equals(requestMethod) && !HttpMethod.GET.name().equals(requestMethod)) {
            NativeWebRequest webRequest = new ServletWebRequest(request);
            Map<?, ?> pathVariables = (Map)webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, 0);
            xOperLog.setOperParam(StringTools.substring(pathVariables.toString(), 0, 2000));
        } else {
            String params = ServletUtils.argsArrayToString(joinPoint.getArgs());
            xOperLog.setOperParam(StringTools.substring(params, 0, 2000));
        }

    }
}
